package com.yicong.ants.ui.me;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.me.sign.SignItem;
import com.yicong.ants.databinding.SignActivityBinding;
import com.yicong.ants.databinding.SignListItemBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cchao
 * @version 2019-07-11.
 */
public class ActivitySignActivity extends BaseTitleBarActivity<SignActivityBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.sign_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("活动签到");
        mDataBind.setClick(this);
        onLoadData();
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Sign_List_Update:
                onLoadData();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        Logs.d("onActivityResult " + requestCode + resultCode + JsonHelper.toJson(data));
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == Constants.Activity_Req_Code.Scan) {
            String content = data.getStringExtra(Constant.CODED_CONTENT);
            Logs.d("scan", "获取到扫码结果 " + content);
            handleScanResult(content);
        }
    }

    private void handleScanResult(String content) {
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", BusinessHelper.mActivityId);
        map.put("content", content);
        showProgress();
        addSubscribe(RetrofitHelper.getLineApi().scanSignQr(map)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                BusinessHelper.mSignScanContent = content;
                Dialogs.showSignSingle(ActivitySignActivity.this, respBean.getData());
            }, RxHelper.getHideProgressConsumer(this)));
    }

    @Override
    protected void onLoadData() {
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", BusinessHelper.mActivityId);
        showProgress();
        addSubscribe(RetrofitHelper.getLineApi().activityDetail(map)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                mDataBind.setBean(respBean.getData());
                updateSignedList(respBean.getData().getSigns());
            }, RxHelper.getHideProgressConsumer(this)));
    }

    private void updateSignedList(List<SignItem> signs) {
        mDataBind.signList.removeAllViews();

        for (SignItem child : signs) {
            SignListItemBinding b = DataBindingUtil.inflate(mLayoutInflater
                ,R.layout.sign_list_item,mDataBind.signList,true);
            b.setBean(child);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan:
                BusinessHelper.mSignScanContent = "";
                addSubscribe(new RxPermissions(this)
                    .request(Constants.CAMERA_PHOTO)
                    .subscribe(aBoolean -> {
                        if (!aBoolean) {
                            return;
                        }
                        Intent intent = new Intent(thisActivity, CaptureActivity.class);
                        /* ZxingConfig是配置类 可以设置是否显示底部布局，闪光灯，相册，*/
                        ZxingConfig config = new ZxingConfig();
//                        config.setPlayBeep(true);//是否播放扫描声音 默认为true
                        // config.setShake(true);//是否震动  默认为true
                        config.setDecodeBarCode(true);//是否扫描条形码 默认为true
                        config.setReactColor(R.color.white);//设置扫描框四个角的颜色 默认为白色
                        config.setFrameLineColor(R.color.white);//设置扫描框边框颜色 默认无色
                        config.setScanLineColor(R.color.white);//设置扫描线的颜色 默认白色
                        config.setFullScreenScan(true);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                        startActivityForResult(intent, Constants.Activity_Req_Code.Scan);
                    }));
                break;
            case R.id.more:
                Router.turnTo(mContext, SignListActivity.class)
                    .putExtra(Constants.Extra.Id, BusinessHelper.mActivityId)
                    .start();
                break;
        }
    }
}
